package com.source.lightfowl.basedata.controller;

import com.source.lightfowl.VO.BarcodeMachineVO;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.BarcodeMachineMsg;
import com.source.lightfowl.basedata.entity.Barcode_Machine_Status;
import com.source.lightfowl.basedata.entity.MachineStatusList;
import com.source.lightfowl.basedata.service.BarcodeMachineChartService;
import com.source.lightfowl.basedata.service.BarcodeMachineMsgService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Optional;

/**
 * @Description:
 * @Author: xcy
 * @CreateTime: 2020/9/12 19:51
 */
@Api(tags = "所有的脚标机")
@CrossOrigin
@RestController
@RequestMapping(value = "/MachineMsg")
@SuppressWarnings("all")
public class BarcodeMachineMsgController {
    @Autowired
    private BarcodeMachineMsgService service;


    @ApiOperation(value = "查询全部数据")
    //查找全部
    @GetMapping("/barcodeMachineMsg")
    public MyResult findAll(){

        List<BarcodeMachineMsg> all = service.findAll();
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    @ApiOperation(value = "统计所有脚标机的数量")
    //查找全部
    @GetMapping("/barcodeMachineMsgCount")
    public MyResult getCount(){

        Long all = service.getCount();
        if(all != null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }


    //查找 分页
    /*
     */
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/barcodeMachineMsgFindAllPage")
    public MyResult findAllPage(int page,int pageNum){

        Page<BarcodeMachineMsg> all = service.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    /*
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/barcodeMachineMsg/id")
    public MyResult findById(String id){
        Optional<BarcodeMachineMsg> house = service.findById(id);
        if(house.isPresent())
            return MyResult.build(0,"成功",house.get());
        else
            return MyResult.build(1,"结果为空");
    }

    //增加 修改
    @ApiOperation(value = "增加/修改 数据 ")
    @PostMapping("/barcodeMachineMsg")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(BarcodeMachineMsg barcodeMachine, MultipartFile file, HttpServletRequest request) throws Exception {
        service.save(barcodeMachine,file,request);
        return MyResult.build(0,"成功");

    }




    //删除
    @ApiOperation("根据id删除数据")
    @DeleteMapping("/barcodeMachineMsg/id")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(String id){
        try{
            service.deleteById(id);
            return MyResult.build(0,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.build(1,"删除操作出现异常");
        }
    }



    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/barcodeMachineMsgDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的

        try {
            service.deleteBatch(ids);
            return MyResult.build(0,"批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "批量删除失败");
        }

    }


    //下面这个接口service没有实现 有需要再改
    //单纯删除图片 不做别的
    //@ApiOperation(value = "单纯删除图片")
    //@DeleteMapping("/barMsgImage")
    //@LogAnnotation(operEvent = "操作类型：删除记录",operType = 4)
    public MyResult deleteImag(String mag){    //注意：
        service.deleteImage(mag);
        return MyResult.build(0,"删除成功");

    }



}
